<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/25
 * Time: 13:44
 */

namespace App\Service;
use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\HttpClient\HttpClient;

class LiveService
{
    /**
     * 正在进行lol比赛
     * @return mixed
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public $bet = [
        [
            'source'    => 'pinnacle',
            'team_a'    => [],
            'team_b'    => [],
            'name'      => '平博'
        ],
        [
            'source'    => '雷竞技',
            'team_a'    => [],
            'team_b'    => [],
            'name'      => '雷竞技'
        ],
        [
            'source'    => 'bet365',
            'team_a'    => [],
            'team_b'    => [],
            'name'      => 'bet365'
        ],
        [
            'source'    => 'anggame',
            'team_a'    => [],
            'team_b'    => [],
            'name'      => '安博'
        ]
    ];

    public function lol()
    {
        $matchs = MysqlPool::invoke(function (MysqlObject $db){
            return $db->where('status',1)->orderBy('start_time','asc')->get('siam_lolmatchs');
        });
        if( $matchs ){
            $matchs = MysqlPool::invoke(function (MysqlObject $db)use ($matchs){
                foreach ( $matchs as $k=>$match ){
                    $battle_ids = json_decode($match['battle_ids'],true);
                    $matchs[$k]['match_time'] = $match['start_time'];
                    $matchs[$k]['start_time'] = $this->getMsecToMescdate($match['start_time']);
                    $matchs[$k]['battle_ids'] = $battle_ids;
                    $team_b = $db->where('team_id',$match['team_b_id'])->getOne('siam_lolteams','name,name_en,short_name,logo') ? : [];//队伍a详情
                    $team_a = $db->where('team_id',$match['team_a_id'])->getOne('siam_lolteams','name,name_en,short_name,logo') ? : [];;//队伍b详情
                    $bets = $db->where('match_id',$match['match_id'])->where('bet_type',1)->get('siam_lolbet',null,'options,source');
                    //赔率
                    if( $bets ){
                        foreach ( $bets as $k2=>$v2 ){
                            $options = json_decode($v2['options'],true);
                            if( $options[0]['team_id'] == $match['team_b_id'] ){
                                $bets[$k2]['team_b'] = $options[0];
                            }else{
                                $bets[$k2]['team_a'] = $options[0];
                            }
                            if( $options[1]['team_id'] == $match['team_b_id'] ){
                                $bets[$k2]['team_b'] = $options[1];
                            }else{
                                $bets[$k2]['team_a'] = $options[1];
                            }
                            unset($bets[$k2]['options']);
                            if( $v2['source'] == 'pinnacle' ){
                                $this->bet[0]['source'] = 'pinnacle';
                                $this->bet[0]['name'] = '平博';
                                $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == '雷竞技' ){
                                $this->bet[1]['source'] = '雷竞技';
                                $this->bet[1]['name'] = '雷竞技';
                                $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == 'bet365' ){
                                $this->bet[2]['source'] = 'bet365';
                                $this->bet[2]['name'] = 'bet365';
                                $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == 'anggame' ){
                                $this->bet[3]['source'] = 'anggame';
                                $this->bet[3]['source'] = '安博';
                                $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                            }
                        }
                    }

                    $matchs[$k]['bet'] = $this->bet;
                    $m_team = [
                        'side'      => null,
                        'money'     => null,
                        'is_win'    => null,
                        'battle_id' => null,
                        'kill_count'=> null,
                        'death_count'=> null,
                        'assist_count'=> null,
                        'is_ten_kills'=> null,
                        'is_five_kills'=> null,
                        'is_first_blood'=> null,
                        'is_first_tower'=> null,
                        'big_dragon_count'=> null,
                        'small_dragon_count'=> null,
                        'is_first_big_dragon'=> null,
                        'tower_success_count'=> null,
                        'is_first_small_dragon'=> null,
                    ];

                    $matchs[$k]['team_a'] = array_merge($team_a,$m_team);
                    $matchs[$k]['team_b'] = array_merge($team_b,$m_team);
                    if( $battle_ids ){ //战役详情，获取最后一场战役作为直播数据
                        $i = count($battle_ids)-1;
                        $url = UrlEnums::FJURL.UrlEnums::LOL_LIVE_BATTLE_PATH."?battle_id={$battle_ids[$i]}&version=2";
                        $client = new HttpClient($url);
                        list($s1, $s2) = explode(' ', microtime());
                        $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                        $option = [
                            'path'      => UrlEnums::LOL_LIVE_BATTLE_PATH,
                            'api_time'  => $microtime
                        ];
                        $header = Auth::getInstance()->header($option);
                        $client->setHeaders($header,true,false);
                        $response = $client->get();
                        $content = $response->getBody();
                        $data = json_decode($content,true);
                        if ( isset($data['code']) && $data['code'] == 200 ){

                            $battle = $data['data']; //战役赋值，剔除不必要的数据方便展示
                            foreach ($battle['team_stats'] as $k1=>$v1){
                                unset($v1['ban_ids']);
                                unset($v1['ban_list']);
                                unset($v1['pick_ids']);
                                unset($v1['pick_list']);
                                if( $v1['team_id'] == $match['team_a_id'] ){
                                    $matchs[$k]['team_a'] = $v1;
                                }else{
                                    $matchs[$k]['team_b'] = $v1;
                                }
                            }
                            $matchs[$k]['team_a'] = array_merge($team_a,$matchs[$k]['team_a']);//将队伍信息与队伍战役数据合并
                            $matchs[$k]['team_b'] = array_merge($team_b,$matchs[$k]['team_b']);//将队伍信息与队伍战役数据合并
                            $matchs[$k]['duration'] = floor($battle['duration']/60).'′'.$battle['duration']%60;

                        }
                    }
                    $live_video = $db->where('game','lol')
                        ->where('match_id',$match['match_id'])->get('siam_live_video',null,'name,url');
                    $league = $db->where('league_id',$match['league_id'])->getOne('siam_lolleague','name,name_en,short_name,logo');
                    $matchs[$k]['live_video'] = $live_video;
                    $matchs[$k]['league'] = $league;
                }
                return $matchs;
            });

        }
        return $matchs;
    }

    /**
     * 正在进行dota比赛
     * @return mixed
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function dota()
    {
        $matchs = MysqlPool::invoke(function (MysqlObject $db){
            return $db->where('status',1)->orderBy('start_time','asc')->get('siam_dotamatchs');
        });
        if( $matchs ){
            $matchs = MysqlPool::invoke(function (MysqlObject $db)use($matchs){
                foreach ( $matchs as $k=>$match ){
                    $battle_ids = json_decode($match['battle_ids'],true);
                    $matchs[$k]['battle_ids'] = $battle_ids;
                    $matchs[$k]['start_time'] = $this->getMsecToMescdate($match['start_time']);
                    $team_b = $db->where('team_id',$match['team_b_id'])->getOne('siam_dotateams','name,short_name,logo');
                    $team_a = $db->where('team_id',$match['team_a_id'])->getOne('siam_dotateams','name,short_name,logo');
                    $bets = $db->where('match_id',$match['match_id'])->where('bet_type',1)->get('siam_dotabet',null,'options,source');
                    //赔率
                    foreach ( $bets as $k2=>$v2 ){
                        $options = json_decode($v2['options'],true);
                        if( $options[0]['team_id'] == $match['team_b_id'] ){
                            $bets[$k2]['team_b'] = $options[0];
                        }else{
                            $bets[$k2]['team_a'] = $options[0];
                        }
                        if( $options[1]['team_id'] == $match['team_b_id'] ){
                            $bets[$k2]['team_b'] = $options[1];
                        }else{
                            $bets[$k2]['team_a'] = $options[1];
                        }
                        unset($bets[$k2]['options']);
                        if( $v2['source'] == 'pinnacle' ){
                            $this->bet[0]['source'] = 'pinnacle';
                            $this->bet[0]['name'] = '平博';
                            $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                            $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                        }
                        if( $v2['source'] == '雷竞技' ){
                            $this->bet[1]['source'] = '雷竞技';
                            $this->bet[1]['name'] = '雷竞技';
                            $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                            $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                        }
                        if( $v2['source'] == 'bet365' ){
                            $this->bet[2]['source'] = 'bet365';
                            $this->bet[2]['name'] = 'bet365';
                            $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                            $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                        }
                        if( $v2['source'] == 'anggame' ){
                            $this->bet[3]['source'] = 'anggame';
                            $this->bet[3]['name'] = '安博';
                            $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                            $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                        }
                    }
                    $matchs[$k]['bet'] = $this->bet;
                    $m_team = [
                        'camp'  => null,
                        'score' => null,
                        'is_win'=> null,
                        'battle_id' => null,
                        'kill_count'=> null,
                        'is_ten_kills'  => null,
                        'is_first_blood'=> null,
                        'money' => null
                    ];
                    $matchs[$k]['team_a'] = array_merge($m_team,$team_a);
                    $matchs[$k]['team_b'] = array_merge($m_team,$team_b);

                    //战役ID
                    if( $battle_ids ){
                        $i = count($battle_ids)-1;
                        $url = UrlEnums::FJURL.UrlEnums::DOTA_LIVE_BATTLE_PATH."?battle_id={$battle_ids[$i]}&version=2";
                        list($s1, $s2) = explode(' ', microtime());
                        $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                        $option = [
                            'path'      => UrlEnums::DOTA_LIVE_BATTLE_PATH,
                            'api_time'  => $microtime
                        ];
                        $client = new HttpClient($url);
                        $header = Auth::getInstance()->header($option);
                        $client->setHeaders($header,true,false);
                        $response = $client->get();
                        $content = $response->getBody();
                        $data = json_decode($content,true);
                        if ( isset($data['code']) && $data['code'] == 200 ){

                            foreach ($data['data']['team_stats'] as $k1=>$v1){
                                unset($v1['team']);
                                unset($v1['ban_ids']);
                                unset($v1['ban_list']);
                                unset($v1['pick_ids']);
                                unset($v1['pick_list']);
                                if( $v1['team_id'] == $match['team_a_id'] ){
                                    $matchs[$k]['team_a'] = $v1;
                                }else{
                                    $matchs[$k]['team_b'] = $v1;
                                }
                            }
                            if( $data['data']['players'] ){ //统计金钱
                                $a_money = 0;
                                $b_money = 0;
                                foreach ($data['data']['players'] as $k2=>$v2){
                                    if( $v2['team_id'] == $match['team_a_id'] ){
                                        $a_money += $v2['gold'];
                                    }
                                    if( $v2['team_id'] == $match['team_b_id'] ){
                                        $b_money += $v2['gold'];
                                    }
                                }
                                $matchs[$k]['team_a']['money'] = $a_money;
                                $matchs[$k]['team_b']['money'] = $b_money;
                            }
                            $matchs[$k]['team_a'] = array_merge($matchs[$k]['team_a'],$team_a);
                            $matchs[$k]['team_b'] = array_merge($matchs[$k]['team_b'],$team_b);
                            $matchs[$k]['duration'] = floor($data['data']['duration']/60).'′'.$data['data']['duration']%60;
                        }else{
                            $matchs[$k]['team_stats'] = [];
                        }
                    }
                    $others = MysqlPool::invoke(function (MysqlObject $db)use($match){//直播地址，联赛信息
                        $live_video = $db->where('game','dota')
                            ->where('match_id',$match['match_id'])->get('siam_live_video',null,'name,url');
                        $league = $db->where('league_id',$match['league_id'])->getOne('siam_dotaleague','name,name_en,short_name,logo');
                        return ['live_video'=>$live_video,'league'=>$league];
                    });
                    $matchs[$k]['live_video'] = $others['live_video'];
                    $matchs[$k]['league'] = $others['league'];
                }
                return $matchs;
            });

        }
        return $matchs;
    }

    /**
     * 正在进行csgo比赛
     * @return mixed
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function csgo()
    {
        $lists = MysqlPool::invoke(function (MysqlObject $db){
            return $db->where('status',1)->orderBy('start_time','desc')->get('siam_csgomatchs');
        });
        if( $lists ){
            $lists = MysqlPool::invoke(function (MysqlObject $db)use($lists){
                foreach ($lists as $k => $v){
                    $map_names = $v['map_names'] ? json_decode($v['map_names'],true) : [];
                    $lists[$k]['map_names'] = $map_names;//地图
                    $lists[$k]['map_bp'] = json_decode($v['map_bp'],true);
                    if( $map_names ){
                        foreach ($map_names as $k2=>$v2){
                            if( !strstr($v2,'de_') ){
                                $map_names[$k2] = strtolower('de_'.$v2);
                            }
                        }
                    }
                    $lists[$k]['start_time'] = $this->getMsecToMescdate($v['start_time']);
                    $lists[$k]['team_a'] = $db->where('team_id',$v['team_a_id'])->getOne('siam_csgoteams','team_id,name,logo,country');
                    $lists[$k]['team_b'] = $db->where('team_id',$v['team_b_id'])->getOne('siam_csgoteams','team_id,name,logo,country');
                    $lists[$k]['team_a']['score'] = $v['team_a_score'];
                    $lists[$k]['team_b']['score'] = $v['team_b_score'];
                    $bets = $db->where('match_id',$v['match_id'])->where('bet_type',1)->get('siam_csgobet',null,'options,source');
                    //赔率
                    if( $bets ){
                        foreach ( $bets as $k2=>$v2 ){
                            $options = json_decode($v2['options'],true);
                            if( $options[0]['team_id'] == $v['team_b_id'] ){
                                $bets[$k2]['team_b'] = $options[0];
                            }else{
                                $bets[$k2]['team_a'] = $options[0];
                            }
                            if( $options[1]['team_id'] == $v['team_b_id'] ){
                                $bets[$k2]['team_b'] = $options[1];
                            }else{
                                $bets[$k2]['team_a'] = $options[1];
                            }
                            unset($bets[$k2]['options']);
                            if( $v2['source'] == 'pinnacle' ){
                                $this->bet[0]['source'] = 'pinnacle';
                                $this->bet[0]['name'] = '平博';
                                $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == '雷竞技' ){
                                $this->bet[1]['source'] = '雷竞技';
                                $this->bet[1]['name'] = '雷竞技';
                                $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == 'bet365' ){
                                $this->bet[2]['source'] = 'bet365';
                                $this->bet[2]['name'] = 'bet365';
                                $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == 'anggame' ){
                                $this->bet[3]['source'] = 'anggame';
                                $this->bet[3]['name'] = '安博';
                                $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                            }
                        }
                    }
                    $lists[$k]['bet'] = $this->bet;
                    $fitst_win = '';//第一回合胜利
                    $sixteen_win = '';//第16回合胜利
                    $first_fivewin_round = '';//先胜利5回合
                    if( $map_names ){ //地图列表最后值,作为比赛的直播数据,以请求实时比分事件为准
                        $i = count($map_names)-1;
                        $m_n = $map_names[$i];
                        if( !strstr($map_names[$i],'de_') ){
                            $m_n = strtolower('de_'.$m_n);
                        }
                        $url = UrlEnums::FJURL.UrlEnums::CSGO_LIVE_BATTLE_PATH."?match_id={$v['match_id']}&map_name={$m_n}";
                        list($s1, $s2) = explode(' ', microtime());
                        $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                        $option = [
                            'path'      => UrlEnums::CSGO_LIVE_BATTLE_PATH,
                            'api_time'  => $microtime
                        ];
                        $client = new HttpClient($url);
                        $header = Auth::getInstance()->header($option);
                        $client->setHeaders($header,true,false);
                        $response = $client->get();
                        $content = $response->getBody();
                        $data = json_decode($content,true);
                        if ( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
                            $battle = $data['data'];
                            $terrorist_players = $battle['terrorist_players'];
                            $counter_terrorist_players = $battle['counter_terrorist_players'];

                            $t_round_history_first_half = $battle['terrorist_round_history_first_half'];//t(匪)方上半场
                            $t_round_history_second_half = $battle['terrorist_round_history_second_half'];//t(匪)方下半场

                            $ct_round_history_first_half = $battle['counter_terrorist_round_history_first_half'] ? : [];//ct(警)方上半场
                            $ct_round_history_second_half = $battle['counter_terrorist_round_history_second_half'] ? : [];//ct(警)方上半场
                            if( $ct_round_history_first_half ){
                                $ct_all_round = array_merge_recursive($ct_round_history_first_half,$ct_round_history_second_half);//ct 全场
                            }
                            if( $t_round_history_first_half ){
                                $t_all_round = array_merge_recursive($t_round_history_first_half,$t_round_history_second_half);//t 全场
                            }
                            $t_kill_count = 0;//杀人书
                            $t_death_count = 0;//死亡数
                            $t_assist_count = 0;//助攻数
                            $t_money = 0;//金钱数
                            $ct_kill_count = 0;//杀人书
                            $ct_death_count = 0;//死亡数
                            $ct_assist_count = 0;//助攻数
                            $ct_money = 0;//金钱数

                            $t_first_half_win_nums = 0; //t方上半场胜利次数
                            $t_second_half_win_nums = 0; //t方下半场胜利次数
                            $ct_first_half_win_nums = 0;  //ct方上半场胜利次数
                            $ct_second_half_win_nums = 0; //ct方下半场胜利次数

                            if( $terrorist_players ){ //统计队员匪的所有数据,作为队伍的数据
                                foreach ( $terrorist_players as $player ){
                                    $t_kill_count += $player['kill_count'];
                                    $t_death_count += $player['death_count'];
                                    $t_assist_count += $player['assist_count'];
                                    $t_money += $player['money'];
                                }
                            }
                            if( $counter_terrorist_players ){//统计队员警的所有数据,作为队伍的数据
                                foreach ($counter_terrorist_players as $player){
                                    $ct_kill_count += $player['kill_count'];
                                    $ct_death_count += $player['death_count'];
                                    $ct_assist_count += $player['assist_count'];
                                    $ct_money += $player['money'];
                                }
                            }

                            //上半场 t方
                            if( $t_round_history_first_half ){ //匪
                                foreach ( $t_round_history_first_half as $tfh ){
                                    if( $tfh['type'] != 'lost' ){
                                        $t_first_half_win_nums++;
                                    }
                                }
                            }
                            //下半场 t方
                            if( $t_round_history_second_half ){
                                foreach ( $t_round_history_second_half as $tsh ){
                                    if( $tsh['type'] != 'lost' ){
                                        $t_second_half_win_nums++;
                                    }
                                }
                            }
                            //上半场 ct方
                            if( $ct_round_history_first_half ){ //警
                                foreach ( $ct_round_history_first_half as $ctfh ){
                                    if( $ctfh['type'] != 'lost' ){
                                        $ct_first_half_win_nums++;
                                    }
                                }
                            }
                            //下半场 ct方
                            if( $ct_round_history_second_half ){
                                foreach ( $ct_round_history_second_half as $ctsh ){
                                    if( $ctsh['type'] != 'lost' ){
                                        $ct_second_half_win_nums++;
                                    }
                                }
                            }

                            if( isset($ct_all_round) && isset($t_all_round) ){
                                if( $ct_all_round[0]['type'] != 'lost' ){
                                    $fitst_win = 'CT';//ct第一回合胜利
                                }else{
                                    $fitst_win = 'T';//t第一回合胜利
                                }
                                if( isset($ct_all_round[15]) ){
                                    if( $ct_all_round[15]['type'] != 'lost' ){
                                        $sixteen_win = 'CT';//ct第一回合胜利
                                    }else{
                                        $sixteen_win = 'T';//t第一回合胜利
                                    }
                                }
                                $ct_win = 0;
                                foreach ( $ct_all_round as $v1 ){ //ct上半场筛选达到5回合胜利，所需要的回合总数
                                    if( $v1['type'] != 'lost' ){
                                        $ct_win++;
                                    }
                                    if( $ct_win >= 5 ){
                                        $ct_five_win = $v1['round'];
                                        break;
                                    }else{
                                        $ct_five_win = $v1['round'];
                                    }
                                }
                                $t_win = 0;
                                foreach ( $t_all_round as $v2 ){
                                    if( $v2['type'] != 'lost' ){
                                        $t_win++;
                                    }
                                    if( $t_win >= 5 ){
                                        $t_five_win = $v2['round'];
                                        break;
                                    }else{
                                        $t_five_win = $v2['round'];
                                    }
                                }
                                if( $t_five_win < $ct_five_win ){
                                    $first_fivewin_round = 'T'; //T先胜5回合
                                }else{
                                    $first_fivewin_round = 'CT';//CT先胜5回合
                                }
                            }

                            if( $battle['terrorist_team_id'] == $v['team_a_id'] ){ //team_a 是匪,将警匪归类
                                $lists[$k]['team_a']['is_bomb_planted'] = $battle['is_bomb_planted'];//是否安装炸弹
                                $lists[$k]['team_a']['kill_count'] = $t_kill_count;
                                $lists[$k]['team_a']['death_count'] = $t_death_count;
                                $lists[$k]['team_a']['assist_count'] = $t_assist_count;
                                $lists[$k]['team_a']['money']   = $t_money;
                                $lists[$k]['team_a']['role']   = 'T';
                                $lists[$k]['team_a']['first_half_win_nums'] = $t_first_half_win_nums;//t方上半场
                                $lists[$k]['team_a']['second_half_win_nums'] = $t_second_half_win_nums;//t方下半场

                                $lists[$k]['team_b']['kill_count'] = $ct_kill_count;
                                $lists[$k]['team_b']['death_count'] = $ct_death_count;
                                $lists[$k]['team_b']['assist_count'] = $ct_assist_count;
                                $lists[$k]['team_b']['money']   = $ct_money;
                                $lists[$k]['team_b']['role']   = 'CT';
                                $lists[$k]['team_b']['first_half_win_nums'] = $ct_first_half_win_nums;//t方上半场
                                $lists[$k]['team_b']['second_half_win_nums'] = $ct_second_half_win_nums;//t方下半场
                            }else{ //team_a 是警
                                $lists[$k]['team_b']['is_bomb_planted'] = $battle['is_bomb_planted'];//是否安装炸弹
                                $lists[$k]['team_a']['kill_count'] = $ct_kill_count;
                                $lists[$k]['team_a']['death_count'] = $ct_death_count;
                                $lists[$k]['team_a']['assist_count'] = $ct_assist_count;
                                $lists[$k]['team_a']['money']   = $ct_money;
                                $lists[$k]['team_a']['role']   = 'CT';
                                $lists[$k]['team_a']['first_half_win_nums'] = $ct_first_half_win_nums;//t方上半场
                                $lists[$k]['team_a']['second_half_win_nums'] = $ct_second_half_win_nums;//t方下半场

                                $lists[$k]['team_b']['kill_count'] = $t_kill_count;
                                $lists[$k]['team_b']['death_count'] = $t_death_count;
                                $lists[$k]['team_b']['assist_count'] = $t_assist_count;
                                $lists[$k]['team_b']['money']   = $t_money;
                                $lists[$k]['team_b']['role']   = 'T';
                                $lists[$k]['team_b']['first_half_win_nums'] = $t_first_half_win_nums;//t方上半场
                                $lists[$k]['team_b']['second_half_win_nums'] = $t_second_half_win_nums;//t方下半场
                            }
                        }
                    }
                    $lists[$k]['fitst_win'] = $fitst_win;
                    $lists[$k]['sixteen_win'] = $sixteen_win;
                    $lists[$k]['first_fivewin_round'] = $first_fivewin_round;
                    $lists[$k]['league'] = $db->where('league_id',$v['league_id'])->getOne('siam_csgoleague','league_id,logo,name');
                    unset($lists[$k]['battle_list']);
                    if( $v['bo'] == 1 ){
                        if( $v['team_a_score'] > $v['team_b_score'] ){
                            $lists[$k]['team_a_score'] = 1;
                            $lists[$k]['team_b_score'] = 0;
                        }else{
                            $lists[$k]['team_b_score'] = 1;
                            $lists[$k]['team_a_score'] = 0;
                        }
                    }
                }
                return $lists;
            });
        }
        return $lists;
    }
    //毫秒

    /**
     * 正在进行的kog比赛
     * @return mixed
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function kog()
    {
        $matchs = MysqlPool::invoke(function (MysqlObject $db){
            return $db->where('status',1)->orderBy('start_time','asc')->get('siam_kogmatchs');
        });
        if( $matchs ){
            $matchs = MysqlPool::invoke(function (MysqlObject $db)use ($matchs){
                foreach ( $matchs as $k=>$match ){
                    $battle_ids = json_decode($match['battle_ids'],true);
                    $matchs[$k]['match_time'] = $match['start_time'];
                    $matchs[$k]['battle_ids'] = $battle_ids;
                    $matchs[$k]['start_time'] = $this->getMsecToMescdate($match['start_time']);
                    $team_b = $db->where('team_id',$match['team_b_id'])->getOne('siam_kogteams','name,name_en,short_name,logo') ? : [];//队伍a详情
                    $team_a = $db->where('team_id',$match['team_a_id'])->getOne('siam_kogteams','name,name_en,short_name,logo') ? : [];;//队伍b详情
                    $bets = $db->where('match_id',$match['match_id'])->where('bet_type',1)->get('siam_kogbet',null,'options,source');
                    if( $bets ){
                        foreach ( $bets as $k2=>$v2 ){
                            $options = json_decode($v2['options'],true);
                            if( $options[0]['team_id'] == $match['team_b_id'] ){
                                $bets[$k2]['team_b'] = $options[0];
                            }else{
                                $bets[$k2]['team_a'] = $options[0];
                            }
                            if( $options[1]['team_id'] == $match['team_b_id'] ){
                                $bets[$k2]['team_b'] = $options[1];
                            }else{
                                $bets[$k2]['team_a'] = $options[1];
                            }
                            unset($bets[$k2]['options']);
                            if( $v2['source'] == 'pinnacle' ){
                                $this->bet[0]['source'] = 'pinnacle';
                                $this->bet[0]['name'] = '平博';
                                $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == '雷竞技' ){
                                $this->bet[1]['source'] = '雷竞技';
                                $this->bet[1]['name'] = '雷竞技';
                                $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == 'bet365' ){
                                $this->bet[2]['source'] = 'bet365';
                                $this->bet[2]['name'] = 'bet365';
                                $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                            }
                            if( $v2['source'] == 'anggame' ){
                                $this->bet[3]['source'] = 'anggame';
                                $this->bet[3]['name'] = '安博';
                                $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                                $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                            }
                        }
                    }
                    $matchs[$k]['bet'] = $this->bet;
                    $m_team = [
                        'side'      => null,
                        'money'     => null,//经济
                        'is_win'    => null,
                        'battle_id' => null,
                        'kill_count'=> null,
                        'death_count'=> null,
                        'assist_count'=> null,
                        'big_dragon_count'=> null,//主宰数
                        'small_dragon_count'=> null,//暴君数
                        'tower_success_count'=> null, //拆塔
                    ];
                    $matchs[$k]['live_video'] = [];
                    $matchs[$k]['league'] = $db->where('league_id',$match['league_id'])->getOne('siam_kogleague','name,short_name,logo');//联赛信息
                    $matchs[$k]['team_a'] = array_merge($team_a,$m_team);
                    $matchs[$k]['team_b'] = array_merge($team_b,$m_team);
                }
                return $matchs;
            });
        }
        return $matchs;
    }

    public function getMsecToMescdate($msectime)
    {
        $msectime = $msectime * 0.001;
        if(strstr($msectime,'.')){
            sprintf("%01.3f",$msectime);
            list($usec, $sec) = explode(".",$msectime);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT);
        }else{
            $usec = $msectime;
            $sec = "000";
        }
        $date = date("Y-m-d H:i:s",$usec);
        return $mescdate = str_replace('x', $sec, $date);
    }

}